// var loadWebIndex = null;
// layui.use(['layer'], function () {
//     var layer = layui.layer;
//     loadWebIndex = layer.load(1, {
//         shade: [0.1,'#fff'] //0.1透明度的白色背景
//     });
// });
/* 显示遮罩层 */
function showOverlay() {
    var overlay = $("#overlay");
    overlay.height(document.body.scrollHeight);
    overlay.width(document.body.scrollWidth);

    // fadeTo第一个参数为速度，第二个为透明度
    // 多重方式控制透明度，保证兼容性，但也带来修改麻烦的问题
    overlay.fadeTo(200, 0.5);
}
showOverlay();
/* 隐藏覆盖层 */
function hideOverlay() {
    $("#overlay").fadeOut(200);
}

$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        tabs:null,
        editIndex:null,
        tchHistory:[
            // {sStartTime:'1995-12',sEndTime:'1998-12',sName:'蓝翔学院',sExtraName:'拖拉机',sMajor:'拖拉机专业',mDesc:'拖拉机专业资格证'},
            // {sStartTime:'1998-12',sEndTime:'2004-12',sName:'资深蓝翔学院',sExtraName:'资深拖拉机',sMajor:'资深拖拉机专业',mDesc:'资深拖拉机专业资格证'},
            // {sStartTime:'2004-12',sEndTime:'2016-12',sName:'高级蓝翔学院',sExtraName:'高级拖拉机',sMajor:'高级拖拉机专业',mDesc:'高级拖拉机专业资格证'}
        ],
        workHistory:[
            // {sStartTime:'1995-12',sEndTime:'1998-12',sName:'蓝翔拖拉机公司',sExtraName:'拖拉机攻城狮',sCertifier:'蓝翔高级教师',sPhone:'123456',mDesc:'与公司理念不和'}
        ],
        itemHistory:[
            // {sStartTime:'1995-12',sEndTime:'1998-12',sName:'项目实测',mDesc:'该开发项目即为严格'}
        ],
        family:[
            // {sRelation:'父子',sName:'dsa',sCompany:'dasd',sPhone:'1232'}
        ],
        init:function () {
            this.tabs = $('.tabs').eq(0);
            this.funObj().onLoadSuccess();
            this.listener();
        },
        listener:function () {
            var $this = this;
            //上一步
            $('#preStep').on('click',function () {
                $this.funObj().goToPrev();
            });
            //下一步
            $('#nextStep').on('click',function () {
                $this.funObj().goToNext();
            });


            if ($('#comment').length > 0) {
                $("#comment").click(function () {
                    $this.funObj().goTo('#commentPanel','slide','right');
                });
            }

        },
        funObj:function () {
            var $this = this;
            return {
                onLoadSuccess:function () {
                    $('.tabs-wrap').eq(0).hide();
                    $this.funObj().checkEnable();
                    hideOverlay();
                },
                goToPrev:function () {
                    var prevDom = $this.tabs.find('.tabs-selected').prev().find('a');
                    if (prevDom.length > 0) {
                        prevDom.click();
                    }
                    $this.funObj().checkEnable();
                    $this.funObj().setMainTitle();
                },
                goToNext:function () {
                    var nextDom = $this.tabs.find('.tabs-selected').next().find('a');
                    if (nextDom.length > 0) {
                        nextDom.click();
                    }
                    $this.funObj().checkEnable();
                    $this.funObj().setMainTitle();
                },
                checkEnable:function () {
                    var prevDom = $this.tabs.find('.tabs-selected').prev().find('a');
                    var nextDom = $this.tabs.find('.tabs-selected').next().find('a');
                    if (prevDom.length > 0) {
                        if (xhExtra.buttonDisabled($('#preStep'))) {
                            xhExtra.enableButton($('#preStep'), 'eu');
                        }
                    } else {
                        xhExtra.disableButton($('#preStep'), 'eu');
                    }
                    if (nextDom.length > 0) {
                        if (xhExtra.buttonDisabled($('#nextStep'))) {
                            xhExtra.enableButton($('#nextStep'), 'eu');
                        }
                    } else{
                        xhExtra.disableButton($('#nextStep'), 'eu');
                    }
                },
                setMainTitle:function () {
                    var tab = $this.tabs.find('.tabs-selected');
                    $('#mainPanel-title').html(tab.eq(0).text());
                },
                goTo:function (id) {
                    $.mobile.go(id);
                    $this.editIndex = null;
                },
                saveComment:function () {
                    var params = $('#commentPanel').find('form').serialize();
                    params = decodeURIComponent(params, true);
                    params = strToObj(params);
                    console.log(params);
                    if (params.mSuggest != '复试') {
                        askConfirm('当前建议不为复试,确定结束此人的面试?',function () {
                            $.ajax({
                                url: baseUrl + 'hrm/resource/saveInterview',
                                type: 'POST',
                                data: params,
                                dataType: 'json',
                                complete: function(XMLHttpRequest){
                                    var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
                                    if (result.code == 200) {
                                        showSuccess(result.msg);
                                    } else {
                                        showError(result.error)
                                    }
                                }
                            });
                        })
                    } else {
                        $.ajax({
                            url: baseUrl + 'hrm/resource/saveInterview',
                            type: 'POST',
                            data: params,
                            dataType: 'json',
                            complete: function(XMLHttpRequest){
                                var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
                                if (result.code == 200) {
                                    showSuccess(result.msg);
                                } else {
                                    showError(result.error)
                                }
                            }
                        });
                    }

                }

            }
        }
    }
};

/**
 * form serilize() 对象转obj
 *
 * @param str
 * @returns {any}
 */
function strToObj(str){
    str = str.replace(/&/g,"','");
    str = str.replace(/=/g,"':'");
    str = "({'"+str +"'})";
    return  eval(str);
}

/**
 * 询问
 *
 * @param title
 * @param callback
 *
 */
function askConfirm(title,callback) {
    swal({
        title:title,
        type: 'info',
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        onOpen:function () {
            if ($('.swal2-spacer').length > 0) {
                $('.swal2-spacer').hide();
            }
        }
    }).then(function () {
        if(callback){
            callback.call(this);
        }
    },function () {
        return false;
    });
}

function showSuccess(title) {
    swal({
        title:title,
        type: 'success',
        confirmButtonText: '确定',
        onOpen:function () {
            if ($('.swal2-spacer').length > 0) {
                $('.swal2-spacer').hide();
            }
        }
    }).then(function () {
        return false;
    },function () {
        return false;
    });
}
function showError(msg) {
    swal({
        title:msg,
        type: 'error',
        confirmButtonText: '确定',
        onOpen:function () {
            if ($('.swal2-spacer').length > 0) {
                $('.swal2-spacer').hide();
            }
        }
    }).then(function () {
        return false;
    },function () {
        return false;
    });
}

/**
 * easyui 日期格式只要年月
 *
 *
 */
function monthFormat() {
    //日期选择对象
    $('.month').datebox({
        //显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
        onShowPanel: function () {
            //触发click事件弹出月份层
            var p = $(this).datebox('panel'),
                //日期选择对象中月份
                tds = false,
                //显示月份层的触发控件
                span = p.find('span.calendar-text'),
                kthis = $(this);
            span.trigger('click');
            if (!tds)
            //延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                setTimeout(function() {
                    tds = p.find('div.calendar-menu-month-inner td');
                    tds.click(function(e) {
                        //禁止冒泡执行easyui给月份绑定的事件
                        e.stopPropagation();
                        var month = parseInt($(this).attr('abbr'), 10);
                        var year = p.find('.calendar-menu-year').val();
                        //隐藏日期对象
                        kthis.datebox('hidePanel')
                        //设置日期的值
                            .datebox('setValue', year + '-' + month);
                    });
                }, 0);
        },
        //配置parser，返回选择的日期
        parser: function (s) {
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },
        //配置formatter，只返回年月 之前是这样的d.getFullYear() + '-' +(d.getMonth());
        formatter: function (d) {
            var currentMonth = (d.getMonth()+1);
            var currentMonthStr = currentMonth < 10 ? ('0' + currentMonth) : (currentMonth + '');
            return d.getFullYear() + '-' + currentMonthStr;
        }
    });
}